import { createRouter, createWebHistory } from "vue-router";
import { routes } from "./router";
import store from "@/store";

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  const isLoggedIn = !!token;
  
  // 如果用户未登录且访问的不是登录页，则重定向到登录页
  if (!isLoggedIn && to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
});

export default router;
